package web

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"shendaNFT/pkg/client"
	"shendaNFT/pkg/web/controller"
)

type Server struct {
	ginServer  *gin.Engine
	controller controller.Controller
}

func New(client *client.EthClient, logger *zap.Logger) Server {
	server := Server{
		ginServer:  gin.Default(),
		controller: controller.New(client, logger),
	}
	server.ginServer.Use(cors.Default())
	server.api()

	return server
}

func (s *Server) api() {
	api := s.ginServer.Group("/api")
	api.GET("/wallet/generate", s.controller.GenerateWallet)

	api.POST("/contract/deploy", s.controller.Deploy)
	api.POST("/contract/deployBatch", s.controller.DeployBatch)
	api.POST("/contract/mint", s.controller.Mint)
	api.POST("/contract/mintBatch", s.controller.MintBatch)
	api.POST("/contract/transferFrom", s.controller.TransferFrom)
	api.POST("/contract/transferFromBatch", s.controller.TransferFromBatch)
	api.POST("/contract/burn", s.controller.Burn)

	// 合约的查询接口
	api.GET("/contract/ownerOf", s.controller.OwnerOf)
	api.GET("/contract/totalSupply", s.controller.TotalSupply)
	// 区块数据查询接口
	api.GET("/blockchain/getBalance", s.controller.GetBalance)
	api.GET("/blockchain/getBlockNumber", s.controller.GetBlockNumber)
	api.GET("/blockchain/getTransaction", s.controller.GetTransaction)
	api.GET("/blockchain/getTransactionInBlock", s.controller.GetTransactionInBlock)
	api.GET("/blockchain/getBlockByNumber", s.controller.GetBlockByNumber)
	api.GET("/blockchain/getBlockByHash", s.controller.GetBlockByHash)
}

func (s *Server) Run() {
	err := s.ginServer.Run()
	if err != nil {
		return
	}
}
